//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "LogGrid.h"
using namespace Cephei::QL::Math;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Math/TransformedGrid.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::CLogGrid::CLogGrid (Cephei::QL::Math::IArray^ grid) : CTransformedGrid(CLogGrid::typeid)
{
    CArray^ _Cgrid;
    try
    {
#ifdef HANDLE
        _phLogGrid = NULL;
#endif
        _Cgrid = safe_cast<CArray^> (grid);
        _Cgrid->Lock();
        QuantLib::Array& _grid = static_cast<QuantLib::Array&> (_Cgrid->GetReference ()); 
        _ppLogGrid = new boost::shared_ptr<QuantLib::LogGrid> (new QuantLib::LogGrid ( _grid ));
        SetTransformedGrid (boost::dynamic_pointer_cast<QuantLib::TransformedGrid> (*_ppLogGrid));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cgrid != nullptr) _Cgrid->Unlock();
    }
}
Cephei::QL::Math::CLogGrid::CLogGrid (boost::shared_ptr<QuantLib::LogGrid>& childNative, Object^ owner) : CTransformedGrid(CLogGrid::typeid)
{
#ifdef HANDLE
	_phLogGrid = NULL;
#endif
	_ppLogGrid = &childNative;
    _ppTransformedGrid = new boost::shared_ptr<QuantLib::TransformedGrid> (boost::dynamic_pointer_cast<QuantLib::TransformedGrid> (*_ppLogGrid));
}
Cephei::QL::Math::CLogGrid::CLogGrid (QuantLib::LogGrid& childNative, Object^ owner) : CTransformedGrid(CLogGrid::typeid)
{
#ifdef HANDLE
	_phLogGrid = NULL;
#endif
	_ppLogGrid = new boost::shared_ptr<QuantLib::LogGrid> (&childNative);
    _ppTransformedGrid = new boost::shared_ptr<QuantLib::TransformedGrid> (boost::dynamic_pointer_cast<QuantLib::TransformedGrid> (*_ppLogGrid));
    _LogGridOwner = owner;
    _TransformedGridOwner = owner;
}

Cephei::QL::Math::CLogGrid::CLogGrid (CLogGrid^ copy) : CTransformedGrid(CLogGrid::typeid)
{
#ifdef HANDLE
	_phLogGrid = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLogGrid = new boost::shared_ptr<QuantLib::LogGrid> (copy->GetShared());
        _ppTransformedGrid = new boost::shared_ptr<QuantLib::TransformedGrid> (boost::dynamic_pointer_cast<QuantLib::TransformedGrid> (*_ppLogGrid));
    }
}
Cephei::QL::Math::CLogGrid::CLogGrid (PLATFORM::Type^ t) : CTransformedGrid(CLogGrid::typeid)
{
#ifdef HANDLE
	_phLogGrid = NULL;
#endif
	if (!t->IsSubclassOf(CLogGrid::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::CLogGrid::CLogGrid (QuantLib::Handle<QuantLib::LogGrid>& childNative, Object^ owner)  : CTransformedGrid(CLogGrid::typeid)
{
	_phLogGrid = &childNative;
	_ppLogGrid = &static_cast<boost::shared_ptr<QuantLib::LogGrid>>(childNative.currentLink());
    _ppTransformedGrid = new boost::shared_ptr<QuantLib::TransformedGrid> (boost::dynamic_pointer_cast<QuantLib::TransformedGrid> (*_ppLogGrid));
    _LogGridOwner = owner;
}
Cephei::QL::Math::CLogGrid::CLogGrid (QuantLib::Handle<QuantLib::LogGrid> childNative)  : CTransformedGrid(CLogGrid::typeid)
{
	_phLogGrid = &childNative;
	_ppLogGrid = &static_cast<boost::shared_ptr<QuantLib::LogGrid>>(childNative.currentLink());
    _ppTransformedGrid = new boost::shared_ptr<QuantLib::TransformedGrid> (boost::dynamic_pointer_cast<QuantLib::TransformedGrid> (*_ppLogGrid));
}
#endif
#ifdef STRUCT
Cephei::QL::Math::CLogGrid::CLogGrid (QuantLib::LogGrid childNative)  : CTransformedGrid(CLogGrid::typeid)
{
#ifdef HANDLE
	_phLogGrid = NULL;
#endif
	_ppLogGrid = new boost::shared_ptr<QuantLib::LogGrid> (new QuantLib::LogGrid (childNative));
    _ppTransformedGrid = new boost::shared_ptr<QuantLib::TransformedGrid> (boost::dynamic_pointer_cast<QuantLib::TransformedGrid> (*_ppLogGrid));
}
#endif

Cephei::QL::Math::CLogGrid::~CLogGrid ()
{
    if (_ppLogGrid != NULL)
    {
	    delete _ppLogGrid;
        _ppLogGrid = NULL;
    }
}
Cephei::QL::Math::CLogGrid::!CLogGrid ()
{
    if (_ppLogGrid != NULL)
    {
	    delete _ppLogGrid;
    }
}
QuantLib::LogGrid& Cephei::QL::Math::CLogGrid::GetReference ()
{
    if (_ppLogGrid == NULL) throw REFNEW NativeNullException ();
	return **_ppLogGrid;
}
boost::shared_ptr<QuantLib::LogGrid>& Cephei::QL::Math::CLogGrid::GetShared ()
{
    if (_ppLogGrid == NULL) throw REFNEW NativeNullException ();
	return *_ppLogGrid;
}
QuantLib::LogGrid* Cephei::QL::Math::CLogGrid::GetPointer ()
{
    if (_ppLogGrid == NULL) throw REFNEW NativeNullException ();
	return &**_ppLogGrid;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LogGrid>& Cephei::QL::Math::CLogGrid::GetHandle ()
{
	if (_phLogGrid == NULL)
	{
		_phLogGrid = new Handle<QuantLib::LogGrid> (*_ppLogGrid);
	}
	return *_phLogGrid;
}
#endif
bool Cephei::QL::Math::CLogGrid::HasNative () 
{
	return (_ppLogGrid != NULL);
}

Double Cephei::QL::Math::CLogGrid::LogGrid (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLogGrid)->logGrid ( _i );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IArray^ Cephei::QL::Math::CLogGrid::LogGridArray::get ()
{
    try
    {
    	QuantLib::Array & _rv = (QuantLib::Array &)(*_ppLogGrid)->logGridArray ( );   
        Cephei::QL::Math::CArray^ _nrv = REFNEW Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::ILogGrid^ Cephei::QL::Math::CLogGrid_Factory::Create (Cephei::QL::Math::IArray^ grid)
{
    return REFNEW CLogGrid ( grid);
}
